package com.orm.mybatis.token.config;

import com.orm.mybatis.token.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//配置拦截事件
@Configuration
public class TokenWebMvcConfigurer implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;

    /**
     * 配置跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //定义哪些URL接受跨域
        registry.addMapping("/**")
                //定义哪些origin可以跨域请求
                .allowedOriginPatterns("*")
                //定义接受的跨域请求方法
                .allowedMethods("POST", "GET", "PUT", "PATCH", "OPTIONS", "DELETE")
                .exposedHeaders("Set-Token")
                .allowCredentials(true)
                .allowedHeaders("*")
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor(tokenInterceptor)
             .addPathPatterns("/**")
             .excludePathPatterns("/login/**");
    }
}
